<?php

namespace app\api\controller;

use app\api\model\MerchantTeam as MerchantTeamModel;
use app\api\model\UserTwo as UserModel;
use think\db;

/**
 * 优惠券中心
 * Class Coupon
 * @package app\api\controller
 */
class Merchantteam extends Controller
{
	
	/* @var UserCouponModel $model */
    private $model;

    /* @var \app\api\model\User $model */
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function initialize()
    {
        //parent::initialize();
        //$this->model = new UserCouponModel;
        $this->user = $this->getShangUser();   // 商户信息
        $this->imgadess = $this->pcUrl();
    }

    /**
     * 列表
     */
    public function lists()
    {
        $model = new MerchantTeamModel();
        $list = $model->aaa($this->user['merchant_id']);
        if(!empty($this->user['logo'])){
            $this->user['logoad'] = $this->imgadess.$this->user['logo'].'.jpg';
        }else{
            $this->user['logoad'] = '';
        }
        if(count($list)>0){
            foreach ($list as $key=>$value){
                if(!empty($value['logo'])){
                    $list[$key]['logoad'] = $this->imgadess.$value['logo'].'.jpg';
                }else{
                    $list[$key]['logoad'] = '';
                }
            }
        }
        $info['user'] = $this->user;
        $info['list'] = $list;
        $info['sum'] = count($list);

        return $this->renderSuccess('1000','成功',$info);
    }

    /**
     * 下级列表
     */
    public function xialists()
    {
        $data = $this->request->post();
        if(empty($data['id'])){
            return $this->renderError('1001','缺少必须参数');
        }
        $where['merchant_id'] = $data['id'];
        $use = new \app\api\model\UserTwo();
        $uer = $use->detailShang($where);
        $model = new MerchantTeamModel();
        $list = $model->xialists($data['id']);
        if(!empty($uer['logo'])){
            $uer['logoad'] = $this->imgadess.$uer['logo'].'.jpg';
        }else{
            $uer['logoad'] = '';
        }
        if(count($list)>0){
            foreach ($list as $key=>$value){
                if(!empty($value['logo'])){
                    $list[$key]['logoad'] = $this->imgadess.$value['logo'].'.jpg';
                }else{
                    $list[$key]['logoad'] = '';
                }
            }
        }
        $info['user'] = $uer;
        $info['list'] = $list;
        $info['sum'] = count($list);

        return $this->renderSuccess('1000','成功',$info);
    }


    /*
	*手机号拉取验证码
	* @return array
	*/
    public function addYanzm(){
        $model = new UserModel;
        $info = $this->request->post();
        if(empty($info['phone'])){
            return $this->renderError('1004','手机号不能为空',$data = []);
        }
        $userone = $model->userOne($info);
        if(!$userone){
            return $this->renderError('1001','该手机号未注册过',$data = []);
        }
        $userwhere['merchant_user_id'] =$userone['user_id'];
        $userwhere['is_delete'] =['in','2,5'];

        $userlist = $model->userList($userwhere);
        if(count($userlist)<=0){
            return $this->renderError('1001','该用户暂无可用角色',$data = []);
        }
        $code = rand('1000','9999');
        //$code = '1234';
        $this->duanxin($info['phone'],$code);
        $where = ['phone'=>$info['phone'],'code'=>$code,'create_time'=>time()];
        $user=$model->addYanzm($where,$info['phone']);
        if($user == '1002'){
            return $this->renderError('1002','错误',$data = []);
        }elseif($user == '1003'){
            return $this->renderError('1003','验证码15分钟内有效',$data = []);
        }else{
            return $this->renderSuccess('1000','发送成功',$userlist);
        }
    }

    public  function duanxin($phone,$code)
    {
        //$url = 'http://v.juhe.cn/sms/send';//post查询条件
        //$fields = 'postid=75159251697701&id=1&valicode=&temp=0.006414744938973982&type=zhongtong&phone=&token=&platform=MWWW';
        //$fields = 'mobile=18510852363&tpl_id=176606&tpl_value=%23code%23%3D654654&key=9cace9e00efd9d51f12f2c255e4a40be';
        //$url = 'http://106.14.255.200:8888/sms.aspx';
        //$fields = 'action=send&userid=12&account=艺人装修&password=密码&mobile=18510852363&content=12345678912&sendTime=&extno=';
        $url = 'http://utf8.api.smschinese.cn/';
        $fields = 'Uid=艺人装修平台&Key=d41d8cd98f00b204e980&smsMob='.$phone.'&smsText=【艺人装修】验证码是'.$code;
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_HTTPHEADER => array(
                "accept: */*",
                "accept-encoding: gzip, deflate",
                "accept-language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,ja;q=0.2",
                "cache-control: no-cache",
                "connection: keep-alive",
                "content-type: application/x-www-form-urlencoded",
                //"cookie: $cookie",
                "origin: http://www.**.com",
                "pragma: no-cache",
                "referer: http://www.****.com",
                "user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
                "x-requested-with: XMLHttpRequest"
            ),
        ));

//url
        curl_setopt($curl, CURLOPT_URL, $url);
//post fields
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($curl, CURLOPT_COOKIE, 'csrftoken=mRRKbKMm34ocwmyBGNSnLp4Tk0zLILC9Tihjg0P2dX0; Hm_lvt_22ea01af58ba2be0fec7c11b25e88e6c=1564563968; WWWID=WWW31AAF56D6B1EBE11C65DA00F8BC534D9; Hm_lpvt_22ea01af58ba2be0fec7c11b25e88e6c=1564566015');
        $response = curl_exec($curl);
        $err = curl_error($curl);
        $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        return $response;
    }


    /*
	*下级入库
	* @return array
	*/
    public function addTuandui(){

        $info = $this->request->post();
        if(empty($info['merchant_id'])||empty($info['code'])||empty($info['phone'])){
            return $this->renderError('1001','参数不能为空');
        }

        $list = DB::table('bfb_merchant_code')->where(['phone'=>$info['phone'],'code'=>$info['code']])->order('create_time','DESC')->find();
        if($list){
            $where['merchant_id'] = $info['merchant_id'];
            $where['state'] = 1;
            $where['create_time'] = time();
            $where['merchant_pid'] = $this->user['merchant_id'];
            $model = new MerchantTeamModel;
            if($model->addTuandi($where)){
                return $this->renderSuccess('1000','成功');
            }else{
                return $this->renderSuccess('1002','失败');
            }
        }
        return $this->renderSuccess('1002','验证码错误');

    }


}